//防止事件冒泡
function stopBubble(e)
{
    if (e && e.stopPropagation)
        e.stopPropagation()
    else
        window.event.cancelBubble=true
}

//查询字符串转对象
function convertQueryString() {
    var url = location.search.substr(1, location.search.length);
    if(url) {
        var queryStrings = url.split("&");
        var result = {};
        for (var i = 0; i < queryStrings.length; i++) {
            var tempArray = queryStrings[i].split("=");
            if (tempArray && tempArray.length > 1) {
                if(result[tempArray[0]]){
                    var temp = result[tempArray[0]];
                    if(temp instanceof Array) {
                        temp.push(tempArray[1]);
                    } else {
                        var array = new Array();
                        array.push(temp, tempArray[1]);
                        result[tempArray[0]] = array;
                    }
                } else {
                    result[tempArray[0]] = tempArray[1];
                }
            }
        }
    }
    return result;
}

//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
    var r = window.location.search.substr(1).match(reg);  //匹配目标参数
    if (r != null) return r[2];
    return null; //返回参数值
}

//把url查询字符串中指定参数改值，如没有则拼接上此参数
function setUrlParam(paramName, paramValue) {
    var oldSearch = location.search.substr(1).trim();
    var array = oldSearch.split("&");
    for(var i = 0; i < array.length; i++) {
        if(array[i] == "") {
            array.splice(i, 1);
            i--;
            continue;
        }
        if(array[i].split("=")[0] == paramName) {
            break;
        }
    }
    array[i] = paramName + "=" + paramValue;
    console.log(array);
    var newSearch = "?" + array.join("&");
    location.href = location.origin+location.pathname+newSearch;
}

//借鉴别人的
function changeURLArg(url,arg,arg_val){
    var pattern=arg+'=([^&]*)';
    var replaceText=arg+'='+arg_val;
    if(url.match(pattern)){
        var tmp='/('+ arg+'=)([^&]*)/gi';
        tmp=url.replace(eval(tmp),replaceText);
        return tmp;
    }else{
        if(url.match('[\?]')){
            return url+'&'+replaceText;
        }else{
            return url+'?'+replaceText;
        }
    }
}

function getCookie(name, dealFunction)  {
    var arr,reg=new RegExp("(^| )"+name+"=([^;]*)(;|$)");
    if(arr=document.cookie.match(reg)) {
        if(dealFunction) {
            return dealFunction(arr[2]);
        }
        // return unescape(arr[2]);
        return arr[2];
    }else{
        return null;
    }
}

function setCookie(name, value, dealFunction)  {
    if(dealFunction) {
        value = dealFunction(value);
    }
    document.cookie=name+"="+value;
}

function delCookie(name, dealFunction) {
    var exp = new Date();
    exp.setTime(exp.getTime() - 1);
    var value=getCookie(name);
    if(value!=null) {
        document.cookie= name + "="+value+";expires="+exp.toGMTString();
    }
    if(value && dealFunction) {
        return dealFunction(value);
    }
    return value;
}